package top.juntower.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import top.juntower.pojo.Backstage.Feedback;
import top.juntower.pojo.Backstage.GameInformations;
import top.juntower.pojo.Backstage.GameTypes;
import top.juntower.pojo.Backstage.Userinfo;
import top.juntower.pojo.GameType;
import top.juntower.pojo.dto.GameInformationDto;
import top.juntower.service.impl.BackstageServiceImpl;
import top.juntower.utils.JsonResult;
import top.juntower.utils.QiniuUtil;

import java.io.FileInputStream;
import java.util.*;

@CrossOrigin
@Api(tags = "后台管理")
@RestController
@RequestMapping("backstage")
public class BackstageController {

    @Autowired
    BackstageServiceImpl backstageService;

    @Autowired
    QiniuUtil qiniuUtil;

    @Value("${qiniu.domain}")
    private String fileDomain;

    //上传图片
    @CrossOrigin
    @ApiOperation(value = "上传图片")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "files", value = "上传图片", dataType = "MultipartFile", required = true),
    })
    @PostMapping("/upload")
    public JsonResult upload(@RequestParam("files") MultipartFile[] files) {
        String pictureList[] = new String[files.length];
        if(files!=null && files.length>0) {
            for (int i = 0; i < files.length; i++) {
                MultipartFile file = files[i];
                if (file.isEmpty()) {
                    return JsonResult.error("请选择文件");
                }
                try {
                    FileInputStream fileInputStream = (FileInputStream) file.getInputStream();
                    String originalFilename = file.getOriginalFilename();
                    String fileExtend = originalFilename.substring(originalFilename.lastIndexOf("."));
                    //默认不指定key的状况下，以文件内容的hash值做为文件名
                    String fileKey = UUID.randomUUID().toString().replace("-", "") + fileExtend;

                    qiniuUtil.upload(fileInputStream, fileKey);

                    String tupain = fileDomain + "/" + fileKey;
                    pictureList[i] = tupain;
                } catch (Exception e) {
                    e.printStackTrace();
                    return JsonResult.error("上传失败");
                }
            }
        }
        return JsonResult.ok(pictureList);
    }

    //上传单张图片
    @CrossOrigin
    @ApiOperation(value = "上传单张图片")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "用户头像图片", dataType = "MultipartFile", required = true),
    })
    @PostMapping("/updateuserimg")
    public JsonResult updateuserimg(@RequestParam("file") MultipartFile file) {
        if (file.isEmpty()) {
            return JsonResult.error("请选择文件");
        }
        try {
            FileInputStream fileInputStream = (FileInputStream) file.getInputStream();
            String originalFilename = file.getOriginalFilename();
            String fileExtend = originalFilename.substring(originalFilename.lastIndexOf("."));
            //默认不指定key的状况下，以文件内容的hash值做为文件名
            String fileKey = UUID.randomUUID().toString().replace("-", "") + fileExtend;
            qiniuUtil.upload(fileInputStream,fileKey);
            String tupain=fileDomain+"/"+fileKey;
            return JsonResult.ok(tupain);
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error("上传失败");
        }

    }

    //后台上传游戏

    @ApiOperation(value = "后台上传游戏")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "giName", value = "游戏名字", dataType = "string", required = true),
            @ApiImplicitParam(name = "gtId", value = "类别Id", dataType = "Integer", required = true),
            @ApiImplicitParam(name = "gjDeveloper", value = "开发厂商", dataType = "string", required = true),
            @ApiImplicitParam(name = "gjOperator", value = "运营厂商", dataType = "string", required = true),
            @ApiImplicitParam(name = "gjSize", value = "游戏大小", dataType = "string", required = true),
            @ApiImplicitParam(name = "gjEdition", value = "游戏版本", dataType = "string", required = true),
            @ApiImplicitParam(name = "giIntroduce", value = "游戏介绍", dataType = "string", required = true),
            @ApiImplicitParam(name = "giTitle", value = "游戏简介", dataType = "string", required = true),
            @ApiImplicitParam(name = "giDetails", value = "游戏详情", dataType = "string", required = true),
            @ApiImplicitParam(name = "giLogo", value = "游戏logo", dataType = "string", required = true),
            @ApiImplicitParam(name = "grImg", value = "轮播图", dataType = "list", required = true),
            @ApiImplicitParam(name = "giArea", value = "游戏地区", dataType = "string", required = true),
            @ApiImplicitParam(name = "giBackdrop", value = "背景图片", dataType = "string", required = true)

    })
    @PostMapping("/addGame")
    public JsonResult addGame(@RequestBody GameInformationDto gameInformation) {
        System.out.println(gameInformation.toString());
        int i = backstageService.addGameInformation(gameInformation);
        if (i>0){
            backstageService.addGameType(gameInformation);
            backstageService.addGameTypeUser(gameInformation);
            backstageService.addGameRotation(gameInformation.getGiId(),gameInformation.getGrImg());
            return JsonResult.ok("ok");
        }else {
            return  JsonResult.error(JsonResult.MISS);
        }
        
    }

    //后台上传游戏
    @ApiOperation(value = "后台上传游戏")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "giName", value = "游戏名字", dataType = "String", required = true),
            @ApiImplicitParam(name = "gtId", value = "类别Id", dataType = "Integer", required = true),
            @ApiImplicitParam(name = "gjDeveloper", value = "开发厂商", dataType = "String", required = true),
            @ApiImplicitParam(name = "gjOperator", value = "运营厂商", dataType = "String", required = true),
            @ApiImplicitParam(name = "gjSize", value = "游戏大小", dataType = "String", required = true),
            @ApiImplicitParam(name = "gjEdition", value = "游戏版本", dataType = "String", required = true),
            @ApiImplicitParam(name = "giIntroduce", value = "游戏介绍", dataType = "String", required = true),
            @ApiImplicitParam(name = "giTitle", value = "游戏简介", dataType = "String", required = true),
            @ApiImplicitParam(name = "giDetails", value = "游戏详情", dataType = "String", required = true),
            @ApiImplicitParam(name = "giLogo", value = "游戏logo", dataType = "String", required = true),
            @ApiImplicitParam(name = "grImg", value = "轮播图", dataType = "list", required = true),
            @ApiImplicitParam(name = "giArea", value = "游戏地区", dataType = "String", required = true),
            @ApiImplicitParam(name = "giBackdrop", value = "背景图片", dataType = "String", required = true)
    })
    @PostMapping("/addGameTwo")
    public JsonResult addGameTwo(String giName,Integer gtId,
                                 String gjDeveloper,String gjOperator,
                                 String gjSize,String gjEdition,
                                String giIntroduce,String giTitle,
                                 String giDetails,String giLogo,
                                 String giArea,String grImg,
                                 String giBackdrop
    ) {
        List<String> list=new ArrayList<>();
        String[] str_ids =  grImg.split(",");
        for (String str_id : str_ids) {
            list.add(str_id);
        }
        Map<String,Object> map = new HashMap<>();
        map.put("giName",giName);
        map.put("gtId",gtId);
        map.put("gjDeveloper",gjDeveloper);
        map.put("gjOperator",gjOperator);
        map.put("gjSize",gjSize);
        map.put("gjEdition",gjEdition);
        map.put("giIntroduce",giIntroduce);
        map.put("giTitle",giTitle);
        map.put("giDetails",giDetails);
        map.put("giLogo",giLogo);
        map.put("giArea",giArea);
        map.put("list",list);
        map.put("giBackdrop",giBackdrop);
        int i = backstageService.addGameInformationTwo(map);
        Integer integer = backstageService.queryId();
        map.put("giId",integer);
        if (i>0){
            backstageService.addGameTypeTwo(map);
            backstageService.addGameTypeUserTwo(map);
            backstageService.addGameRotationTwo(map);
            return JsonResult.ok("ok");
        }else {
            return  JsonResult.error(JsonResult.MISS);
        }
    }


    //后台用户信息
    @ApiOperation(value = "后台用户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "分页页码", dataType = "Integer", required = true),
    })
    @PostMapping("/getUserInfo")
    public JsonResult getUserInfo(Integer pageNum){
        if (pageNum == null){
            return JsonResult.error(JsonResult.NO);
        }
        PageHelper.startPage(pageNum,6);
        List<Userinfo> userInfo = backstageService.getUserInfo(pageNum,6);
        PageInfo<Userinfo> userinfoPageInfo = new PageInfo<>(userInfo);
        if (userinfoPageInfo == null){
            return  JsonResult.error(JsonResult.MISS);
        }else{
            return  JsonResult.ok(userinfoPageInfo);
        }
    }

    //后台用户信息模糊查询
    @ApiOperation(value = "后台用户信息模糊查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "分页页码", dataType = "Integer", required = true),
            @ApiImplicitParam(name = "likeInfo", value = "搜索关键字", dataType = "String", required = true),
    })
    @PostMapping("/getLikeUserInfo")
    public JsonResult getLikeUserInfo(Integer pageNum,String likeInfo){
        if(pageNum == null || likeInfo ==  null){
            return  JsonResult.error(JsonResult.NO);
        }
        PageHelper.startPage(pageNum,6);
        List<Userinfo> userInfo = backstageService.getLikeUserInfo(likeInfo,pageNum,6);
        PageInfo<Userinfo> userinfoPageInfo = new PageInfo<>(userInfo);
        if (userinfoPageInfo == null){
            return  JsonResult.error(JsonResult.MISS);
        }else{
            return  JsonResult.ok(userinfoPageInfo);
        }
    }


    //后台删除用户信息
    @ApiOperation(value = "后台删除用户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uId", value = "用户Id", dataType = "Integer", required = true),
            @ApiImplicitParam(name = "uState", value = "修改状态", dataType = "Integer", required = true),
    })
    @PostMapping("/delUserInfo")
    public JsonResult delUserInfo(Integer pageNum,Integer uId,Integer uState){
        if(uId ==  null || uState == null){
            return  JsonResult.error(JsonResult.NO);
        }
        int i = backstageService.delUserInfo(uId, uState);
        if (i>0){
            return  JsonResult.ok(JsonResult.UPDATE_SUCCESS);
        }else {
            return  JsonResult.error(JsonResult.UPDATE_MISS);
        }
    }

    //后台建议信息
    @ApiOperation(value = "后台建议信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "分页页码", dataType = "Integer", required = true),
    })
    @PostMapping("/getFeedback")
    public JsonResult getFeedback(Integer pageNum){
        if(pageNum ==  null ){
            return  JsonResult.error(JsonResult.NO);
        }
        PageHelper.startPage(pageNum,9);
        List<Feedback> feedbacks = backstageService.getFeedbacks(pageNum,9);
        PageInfo<Feedback> feedbackPageInfo = new PageInfo<>(feedbacks);
        if (feedbackPageInfo == null){
            return  JsonResult.error(JsonResult.MISS);
        }else {
            return  JsonResult.ok(feedbackPageInfo);
        }
    }

    //后台游戏信息
    @ApiOperation(value = "后台游戏信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "分页页码", dataType = "Integer", required = true),
    })
    @PostMapping("/getGameInformations")
    public JsonResult getGameInformations(Integer pageNum){
        if(pageNum ==  null ){
            return  JsonResult.error(JsonResult.NO);
        }
        PageHelper.startPage(pageNum,7);
        List<GameInformations> gameInformations = backstageService.getGameInformations(pageNum, 7);
        PageInfo<GameInformations> feedbackPageInfo = new PageInfo<GameInformations>(gameInformations);
        if (feedbackPageInfo == null){
            return  JsonResult.error(JsonResult.MISS);
        }else {
            return  JsonResult.ok(feedbackPageInfo);
        }
    }

    //后台根据id游戏信息
    @ApiOperation(value = "后台根据id游戏信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "giId", value = "游戏Id", dataType = "Integer", required = true),
    })
    @PostMapping("/getGameInformationById")
    public JsonResult getGameInformationById(Integer giId){
        if(giId ==  null ){
            return  JsonResult.error(JsonResult.NO);
        }
        GameInformations gameInformationById = backstageService.getGameInformationById(giId);

        String grImg = gameInformationById.getGrImg();
        List<String> list=new ArrayList<>();
        String[] str_ids =  grImg.split(",");
        for (String str_id : str_ids) {
            list.add(str_id);
        }
        gameInformationById.setGiList(list);
        if (gameInformationById == null){
            return  JsonResult.error(JsonResult.MISS);
        }else {
            return  JsonResult.ok(gameInformationById);
        }
    }

    //后台修改游戏状态
    @ApiOperation(value = "后台修改游戏状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "giId", value = "游戏Id", dataType = "Integer", required = true),
            @ApiImplicitParam(name = "giState", value = "游戏状态", dataType = "Integer", required = true),
    })
    @PostMapping("/delGaemInformation")
    public JsonResult delGaemInformation(Integer giId,Integer giState){
        if(giId ==  null || giState == null){
            return  JsonResult.error(JsonResult.NO);
        }
        int i = backstageService.delGameInformation(giId, giState);
        if (i > 0){
            return  JsonResult.ok(JsonResult.UPDATE_SUCCESS);
        }else {
            return  JsonResult.error(JsonResult.UPDATE_MISS);
        }
    }

    //后台模糊查询游戏信息
    @ApiOperation(value = "后台模糊查询游戏信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "分页页码", dataType = "Integer", required = true),
            @ApiImplicitParam(name = "likeGame", value = "模糊关键字", dataType = "String", required = true),
    })
    @PostMapping("/getGameInformationLike")
    public JsonResult getGameInformationLike(Integer pageNum,String likeGame){
        if(pageNum ==  null || likeGame == null){
            return  JsonResult.error(JsonResult.NO);
        }
        PageHelper.startPage(pageNum,7);
        List<GameInformations> gameInformationLike = backstageService.getGameInformationLike(likeGame, pageNum, 7);
        PageInfo<GameInformations> feedbackPageInfo = new PageInfo<GameInformations>(gameInformationLike);
        if (feedbackPageInfo == null){
            return  JsonResult.error(JsonResult.MISS);
        }else {
            return  JsonResult.ok(feedbackPageInfo);
        }
    }

    //后台上传游戏
    @ApiOperation(value = "后台上传游戏")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "giId", value = "游戏Id", dataType = "Integer", required = true),
            @ApiImplicitParam(name = "giName", value = "游戏名字", dataType = "String", required = true),
            @ApiImplicitParam(name = "gtId", value = "类别Id", dataType = "Integer", required = true),
            @ApiImplicitParam(name = "gjDeveloper", value = "开发厂商", dataType = "String", required = true),
            @ApiImplicitParam(name = "gjOperator", value = "运营厂商", dataType = "String", required = true),
            @ApiImplicitParam(name = "gjSize", value = "游戏大小", dataType = "String", required = true),
            @ApiImplicitParam(name = "gjEdition", value = "游戏版本", dataType = "String", required = true),
            @ApiImplicitParam(name = "giIntroduce", value = "游戏介绍", dataType = "String", required = true),
            @ApiImplicitParam(name = "giTitle", value = "游戏简介", dataType = "String", required = true),
            @ApiImplicitParam(name = "giDetails", value = "游戏详情", dataType = "String", required = true),
            @ApiImplicitParam(name = "giLogo", value = "游戏logo", dataType = "String", required = true),
            @ApiImplicitParam(name = "grImg", value = "轮播图", dataType = "list", required = true),
            @ApiImplicitParam(name = "giArea", value = "游戏地区", dataType = "String", required = true),
            @ApiImplicitParam(name = "giBackdrop", value = "背景图片", dataType = "String", required = true)
    })
    @PostMapping("/updateGame")
    public JsonResult updateGame(String giName,Integer gtId,
                                 String gjDeveloper,String gjOperator,
                                 String gjSize,String gjEdition,
                                 String giIntroduce,String giTitle,
                                 String giDetails,String giLogo,
                                 String giArea,String grImg,
                                 String giBackdrop,Integer giId
    ) {
        List<String> list=new ArrayList<>();
        String[] str_ids =  grImg.split(",");
        for (String str_id : str_ids) {
            list.add(str_id);
        }
        Map<String,Object> map = new HashMap<>();
        map.put("giId",giId);
        map.put("giName",giName);
        map.put("gtId",gtId);
        map.put("gjDeveloper",gjDeveloper);
        map.put("gjOperator",gjOperator);
        map.put("gjSize",gjSize);
        map.put("gjEdition",gjEdition);
        map.put("giIntroduce",giIntroduce);
        map.put("giTitle",giTitle);
        map.put("giDetails",giDetails);
        map.put("giLogo",giLogo);
        map.put("giArea",giArea);
        map.put("list",list);
        map.put("giBackdrop",giBackdrop);
        map.put("giTime",new Date());
        int i = backstageService.updateGame(map);
        if(i > 0 ){
            backstageService.updateGameDetails(map);
            backstageService.updateGameProperties(map);
            backstageService.deleteGameRotation(map);
            backstageService.addGameRotationTwo(map);
            return JsonResult.ok(JsonResult.UPDATE_SUCCESS);
        }else {
            return JsonResult.error(JsonResult.UPDATE_MISS);
        }

    }

    //后台查询类别信息
    @ApiOperation(value = "后台查询类别信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "分页页码", dataType = "Integer", required = true),
    })
    @PostMapping("/getGameType")
    public JsonResult getGameType(Integer pageNum){
        if(pageNum ==  null ){
            return  JsonResult.error(JsonResult.NO);
        }
        PageHelper.startPage(pageNum,9);
        List<GameTypes> gameType = backstageService.getGameType(pageNum, 9);
        PageInfo<GameTypes> feedbackPageInfo = new PageInfo<GameTypes>(gameType);
        if (feedbackPageInfo == null){
            return  JsonResult.error(JsonResult.MISS);
        }else {
            return  JsonResult.ok(feedbackPageInfo);
        }
    }

    //后台模糊查询类别信息
    @ApiOperation(value = "后台模糊查询类别信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "likeType", value = "搜索关键字", dataType = "String", required = true),
            @ApiImplicitParam(name = "pageNum", value = "分页页码", dataType = "Integer", required = true),
    })
    @PostMapping("/getGameTypeLike")
    public JsonResult getGameTypeLike(Integer pageNum,String likeType){
        if(pageNum ==  null || likeType == null){
            return  JsonResult.error(JsonResult.NO);
        }
        PageHelper.startPage(pageNum,9);
        List<GameTypes> gameTypeLike = backstageService.getGameTypeLike(likeType, pageNum, 9 );
        PageInfo<GameTypes> feedbackPageInfo = new PageInfo<GameTypes>(gameTypeLike);
        if (feedbackPageInfo == null){
            return  JsonResult.error(JsonResult.MISS);
        }else {
            return  JsonResult.ok(feedbackPageInfo);
        }
    }

    //后台根据Id查询类别信息
    @ApiOperation(value = "后台根据Id查询类别信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "gtId", value = "类别Id", dataType = "Integer", required = true),
    })
    @PostMapping("/getGameTypeById")
    public JsonResult getGameTypeById(Integer gtId){
        if(gtId ==  null){
            return  JsonResult.error(JsonResult.NO);
        }
        GameType gameTypeById = backstageService.getGameTypeById(gtId);
        if (gameTypeById == null){
            return  JsonResult.error(JsonResult.MISS);
        }else {
            return  JsonResult.ok(gameTypeById);
        }
    }

    //后台修改类别状态
    @ApiOperation(value = "后台修改类别状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "gtState", value = "类别状态", dataType = "Integer", required = true),
            @ApiImplicitParam(name = "gtId", value = "类别Id", dataType = "Integer", required = true),
    })
    @PostMapping("/updateGameType")
    public JsonResult updateGameType(Integer gtId,Integer gtState){
        if(gtId ==  null || gtState == null){
            return  JsonResult.error(JsonResult.NO);
        }
        int i = backstageService.updateGameType(gtId, gtState);
        if (i < 0 ){
            return  JsonResult.error(JsonResult.UPDATE_MISS);
        }else {
            return  JsonResult.ok(JsonResult.UPDATE_SUCCESS);
        }
    }

    //后台修改类别
    @ApiOperation(value = "后台修改类别")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "gtId", value = "类别Id", dataType = "Integer", required = true),
            @ApiImplicitParam(name = "gtName", value = "类别名字", dataType = "String", required = true),
            @ApiImplicitParam(name = "gtImg", value = "类别图片", dataType = "String", required = true),
    })
    @PostMapping("/updateGameTypes")
    public JsonResult updateGameTypes(Integer gtId,String  gtName,String gtImg){
        if(gtId ==  null || gtName == null || gtImg == null){
            return  JsonResult.error(JsonResult.NO);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("gtId",gtId);
        map.put("gtName",gtName);
        map.put("gtImg",gtImg);
        int i = backstageService.updateGameTypes(map);
        if (i < 0 ){
            return  JsonResult.error(JsonResult.UPDATE_MISS);
        }else {
            return  JsonResult.ok(JsonResult.UPDATE_SUCCESS);
        }
    }

    //后台上传类别
    @ApiOperation(value = "后台上传类别")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "gtName", value = "类别名字", dataType = "String", required = true),
            @ApiImplicitParam(name = "gtImg", value = "类别图片", dataType = "String", required = true),
    })
    @PostMapping("/addGameTypes")
    public JsonResult addGameTypes(String  gtName,String gtImg){
        if(gtName == null || gtImg == null){
            return  JsonResult.error(JsonResult.NO);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("gtName",gtName);
        map.put("gtImg",gtImg);
        int i = backstageService.addGameTypes(map);
        if (i < 0 ){
            return  JsonResult.error(JsonResult.INSERT_MISS);
        }else {
            return  JsonResult.ok(JsonResult.INSERT_SUCCESS);
        }
    }
}
